#include <vector>
#include <pthread.h>
#include <string>
#include <iostream>
#include <unistd.h>

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int cnt = 1;
void* routine(void* args)
{
    std::string name = static_cast<char*>(args);
    pthread_mutex_lock(&lock);
    pthread_cond_wait(&cond, &lock);
    std::cout << "计算：" << cnt << std::endl;
    cnt++;
    pthread_mutex_unlock(&lock);
    return nullptr;
}

int main()
{
    pthread_t t1, t2, t3, t4;
    pthread_create(&t1, nullptr, routine, (void*)"thread - 1");
    pthread_create(&t2, nullptr, routine, (void*)"thread - 2");
    pthread_create(&t3, nullptr, routine, (void*)"thread - 3");
    pthread_create(&t4, nullptr, routine, (void*)"thread - 4");

    std::vector<pthread_t> pths({t1, t2, t3, t4});
    while (true)
    {
        int n = pthread_cond_signal(&cond);
        if(n != 0)
        {
            std::cout << "唤醒失败，线程都执行完毕？" << std::endl;
            break;
        }
        std::cout << n <<std::endl;
        sleep(1);

    }
    
    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);
    pthread_join(t4, nullptr);
    return 0;
}